﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProcedureManager : ManagerBase
{
    #region 字段属性

    /// <summary>
    /// 状态机管理器
    /// </summary>
    private FsmManager m_fsmMgr;

    /// <summary>
    /// 流程状态机
    /// </summary>
    private Fsm<ProcedureManager> m_proFsm;


    /// <summary>
    /// 流程序列
    /// </summary>
    private List<ProcedureBase> m_procedures;


    /// <summary>
    /// 进入的第一流程
    /// </summary>
    private ProcedureBase m_entranceProcedure;


    public ProcedureBase CurProcedureBase
    {
        get
        {
            if (m_proFsm == null)
            {
                Debug.LogError($" 状态机为空 ");
            }

            return (ProcedureBase) m_proFsm.CurrentState;
        }
    }
    
    #endregion

    public ProcedureManager()
    {
        m_fsmMgr = new FsmManager();
        m_proFsm = null;
        m_procedures = new List<ProcedureBase>();
    }

    public override void Init()
    {
        Debug.Log($"{this.GetType().Name} init manaager ....");
    }

    public override void Shutdown()
    {
        // throw new System.NotImplementedException();
    }

    public override void Update(float elapseSeconds, float realElapseSeconds)
    {
        
    }
    
    /// <summary>
    /// 添加流程
    /// </summary>
    /// <param name="peocedure"></param>
    public void AddProcedure(ProcedureBase peocedure)
    {
        if (peocedure== null)
        {
            Debug.LogError("流程为空，无法添加!!");
            return;
        }
        m_procedures.Add(peocedure);
    }
    /// <summary>
    /// 设置入口
    /// </summary>
    /// <param name="procedureBase"></param>
    public void SetEntranceProcedure(ProcedureBase procedureBase)
    {
        m_entranceProcedure = procedureBase;
    }

    /// <summary>
    /// 根据名字设置默认的入口
    /// </summary>
    /// <param name="pro_name"></param>
    public void SetDefaultProcedure(string pro_name)
    {
        foreach (var VARIABLE in m_procedures)
        {
            if (VARIABLE.GetType().FullName == pro_name)
            {
                SetEntranceProcedure(VARIABLE);
                return;
            }
        }
    }
    
    /// <summary>
    /// 创建流程状态机
    /// </summary>
    public void CreateProceduresFsm()
    {
        m_proFsm = m_fsmMgr.CreateFsm(this,"", m_procedures.ToArray());

        if (m_entranceProcedure == null)
        {
            Debug.LogError("入口流程为空，无法开始流程");
            return;
        }

        //开始流程
        m_proFsm.Start(m_entranceProcedure.GetType());
    }
    
}
